package src.org.microblog.dao.impl;

import src.org.microblog.dao.BaseDao;
import src.org.microblog.dao.FollowDao;
import src.org.microblog.entity.Follow;
import src.org.microblog.entity.User;
import src.org.microblog.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FollowDaoImpl extends BaseDao implements FollowDao {
    public FollowDaoImpl(Connection conn){
        super(conn);
    }
    @Override
        public int getFollowedCount(int uId) throws SQLException{
        ResultSet rs = null;
        String sql = "select count(*) from follow where uid=?";
        int count = 0;
        try{
            rs = this.executeQuery(sql,uId);
            while (rs.next()) {
                count = rs.getInt("count(*)");
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return count;
    }

    @Override
    public int getFansCount(int uId) throws SQLException{
        ResultSet rs = null;
        String sql = "select count(*) from follow where followedUid = ?";
        int count = 0;
        try{
            rs = this.executeQuery(sql,uId);
            while (rs.next()) {
                count = rs.getInt("count(*)");
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return count;
    }

    @Override
        public List<User> getFollowedByUidAndKeyword(int uId, String keyword) throws SQLException{
        List<User> list = new ArrayList<User>();
        ResultSet rs = null;
        String sql = "select  user.id, user.nickname, user.sign, user.image, user.address from follow inner join user on follow.followedUid=user.id where follow.uid=? and user.nickname like ?";
        try{
            rs = this.executeQuery(sql,uId,"%"+keyword+"%");
            User user = null;
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setNickname(rs.getString("nickname"));
                user.setSign(rs.getString("sign"));
                user.setImage(rs.getString("image"));
                user.setAddress(rs.getString("address"));
                list.add(user);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return list;
    }

    @Override
    public List<User> getFansByUidAndKeyword(int uId, String keyword) throws SQLException{
        List<User> list = new ArrayList<User>();
        ResultSet rs = null;
        String sql = "select  user.id, user.nickname, user.sign, user.image, user.address from follow inner join user where follow.followedUid =? and follow.uid=user.id and user.nickname like ?";
        try{
            rs = this.executeQuery(sql,uId, "%"+keyword+"%");
            User user = null;
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setNickname(rs.getString("nickname"));
                user.setSign(rs.getString("sign"));
                user.setImage(rs.getString("image"));
                user.setAddress(rs.getString("address"));
                list.add(user);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return list;
    }

    @Override
        public int isFollowed(int uId,int followedUid) throws SQLException{
        boolean isFollowed = false;
        ResultSet rs = null;
            String sql = "select * from follow where uid= ? and followedUid=?";
        try{
            rs = this.executeQuery(sql,uId, followedUid);
            while(rs.next()){
                isFollowed = true;
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return 1;
    }

    @Override
    public int addFollower(Follow follow) throws SQLException{
        String sql = "insert into follow(uid,followedUid) values(?,?)";
        int result = 0;
        try {
            result = this.executeUpdate(sql, follow.getuId(),follow.getFollowerId());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int deleteFollower(Follow follow) throws SQLException {
        String sql = "delete from follow where uid = ? and followedUid = ?";
        int result = 0;
        try {
            result = this.executeUpdate(sql, follow.getuId(),follow.getFollowerId());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
